1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4
5 public
class BGCollectorScript : MonoBehaviour {
6
7     
private GameObject[] backgrounds;
8     
private GameObject[] grounds;
9
10     
private float lastBGX;
11     
private float lastGroundX;
12
13     
// Use this for initialization
14     
void Awake () {
15
16         backgrounds = GameObject.FindGameObjectsWithTag(
"Background");
17         grounds = GameObject.FindGameObjectsWithTag(
"Ground");
18
19         lastBGX = backgrounds[
0].transform.position.x;
20         lastGroundX = grounds[
0].transform.position.x;
21
22         
for(int i = 1; i < backgrounds.Length; i++)
23         {
24             
if(lastBGX < backgrounds[i].transform.position.x)
25             {
26                 lastBGX = backgrounds[i].transform.position.x;
27             }
28         }
29
30         
for (int i = 1; i < grounds.Length; i++)
31         {
32             
if (lastGroundX < grounds[i].transform.position.x)
33             {
34                 lastGroundX = grounds[i].transform.position.x;
35             }
36         }
37     }
38
39     
private void OnTriggerEnter2D(Collider2D target)
40     {
41         
if (target.tag == "Background")
42         {
43             Vector3 temp = target.transform.position;
44             
float width = ((BoxCollider2D)target).size.x;
45
46             temp.x = lastBGX + width;
47             target.transform.position = temp;
48             lastBGX = temp.x;
49         }
50         
else if (target.tag == "Ground")
51         {
52             Vector3 temp = target.transform.position;
53             
float width = ((BoxCollider2D)target).size.x;
54
55             temp.x = lastGroundX + width;
56             target.transform.position = temp;
57             lastGroundX = temp.x;
58         }
59     }
60 }


Gõ tìm kiếm nhanh...